package com.mls.fivedistrict.controller.portal;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.NewsComment;
import com.mls.fivedistrict.server.NewsCommentService;
import com.mls.fivedistrict.server.NewsService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/app")
public class NewsCommentController extends BaseController {
    @Autowired
    NewsCommentService newsCommentService;
    @Autowired
    NewsService newsService;

    /**
     * 评论接口
     *
     * @param newsComment
     * @return
     */
    @PostMapping("/v1/newsComment")
    @Transactional
    public ServiceResponse insertNewsComment(@RequestBody NewsComment newsComment) {
        BeanValidator.check(newsComment);
        newsComment.setUserId(getUser().getId());
        boolean flag = newsCommentService.insert(newsComment);
        Long newsId = newsComment.getNewsId();
        Long parentCommentId = newsComment.getParentCommentId();
        if(flag){
            if(null==parentCommentId){
                //给新闻加评论次数
                flag=newsService.addCommentTimes(newsId);
            }else{
                //给评论加回复次数
                flag=newsCommentService.addReplyTimes(parentCommentId);
            }
        }
        if (flag) {
            return ServiceResponse.createSuccessResponse("评论成功");
        } else {
            return ServiceResponse.createErrorResponse("异常错误,评论失败");
        }
    }

    /**
     * 删除评论
     *
     * @param newsCommentId
     * @return
     */
    @DeleteMapping("/v1/newsComment/{newsCommentId}")
    public ServiceResponse deleteNewsComment(@PathVariable Long newsCommentId) {
        Optional.ofNullable(newsCommentId).orElseThrow(() -> new CustomerException("评论内容id不能为空"));
        Wrapper wrapper=Condition.create();
        wrapper.eq("user_id",getUser().getId());
        wrapper.eq("id",newsCommentId);
        NewsComment newsComment = newsCommentService.selectOne(wrapper);
        Optional.ofNullable(newsComment).orElseThrow(() -> new CustomerException("查无此评论"));
        newsComment.setDelete(true);
        boolean flag = newsCommentService.updateById(newsComment);
        Long newsId = newsComment.getNewsId();
        Long parentCommentId = newsComment.getParentCommentId();
        if(flag){
            //不为0说明是子评论
            if(parentCommentId!=0){
                //父评论回复次数减一
                newsCommentService.cutReplyTimes(parentCommentId);
            }else{
                //新闻评论次数减一
                newsService.cutCommentTimes(newsId);
            }
        }
        if (flag) {
            return ServiceResponse.createSuccessResponse("删除成功");
        } else {
            return ServiceResponse.createErrorResponse("删除失败");
        }
    }


    /**
     * 查询单个评论接口
     *
     * @param newsCommentId
     * @return
     */
    @GetMapping("/v1/newsComment/{newsCommentId}")
    public ServiceResponse getNewsComment(@PathVariable Long newsCommentId) {
        Optional.ofNullable(newsCommentId).orElseThrow(() -> new CustomerException("评论内容id不能为空"));
        NewsComment newsComment = newsCommentService.selectById(newsCommentId);
        if (null != newsComment) {
            return ServiceResponse.createSuccessResponse(newsComment);
        } else {
            return ServiceResponse.createErrorResponse("查无此评论");
        }
    }

    /**
     * 评论列表
     *
     * @return
     */
    @GetMapping("/v1/news/newsComments/{newsId}")
    public ServiceResponse getNewsCommentList(@PathVariable Long newsId) {
        List<NewsComment> newsComment = newsCommentService.getNewsComment(newsId, RequestHolder.getPageFilter());
        PageInfo<NewsComment> newsCommentPageInfo = new PageInfo<NewsComment>(newsComment, 5);
        return ServiceResponse.createSuccessResponse(newsCommentPageInfo);
    }

}
